package com.solute.web.main.index;

import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.solute.web.main.index.service.MainIndexService;
import com.solute.web.main.index.vo.UploadSoluteFile;

@Controller
public class MainIndexController {
	
	private static final Logger logger = LoggerFactory.getLogger(MainIndexController.class);
	
	private @Autowired MainIndexService service;
	private @Autowired ServletContext sc;
	
	@RequestMapping(value="/main/index", method=RequestMethod.GET)
	public ModelAndView main() {
		logger.info("/main/index controller start");
		
		return new ModelAndView("main.index");
	}
	
	@RequestMapping(value="/main/index/ns")
	public @ResponseBody
	Map<String, Integer> getNumSolutions() {
		return service.getNumSolutions();
	}
	
	@RequestMapping(value="/main/index/upload", method=RequestMethod.POST)
	public @ResponseBody
	boolean upload(UploadSoluteFile usf, BindingResult result) {
		
		if(result.hasErrors()) {
			for(ObjectError error : result.getAllErrors()) {
				logger.error("Error : {} - {}", error.getCode(), error.getDefaultMessage());
			}
			return false;
		}
		
		/** DB에 저장 */
		
		return true;
	}
}
